package cmd

import (
	"gitee.com/feimos/dm/core"
	"gitee.com/feimos/dm/utils"
	"github.com/urfave/cli/v2"
	"log"
	"os"
	"path/filepath"
)

// OverviewCommand 处理 overview subcommand
func OverviewCommand(c *cli.Context) error {

	extName := utils.UnifyExtname(core.UnifyPath(c.Args().First()))
	root, err := filepath.Abs(filepath.Dir(os.Args[0]))

	if err != nil {
		return err
	}

	yamlContent, err := core.LoadYaml(root)

	if err != nil {
		log.Println(err)
		log.Fatalln("try to use \"dm init\" to generate config file")
	}

	timeSort := c.Int("time")
	sizeSort := c.Int("size")
	countSort := c.Int("count")
	var overviewResult core.OverviewResult

	if len(extName) == 0 {
		overviewResult = core.GenerateOverview(yamlContent)
	} else {
		overviewResult = core.GenerateOverviewByExt(yamlContent, extName)
	}

	data := core.SortOverviewData(overviewResult, timeSort, sizeSort, countSort, extName)

	utils.PrintOverview(data, extName)

	return nil
}
